package magicbattle.gui;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import magicbattle.game.Game;
import magicbattle.game.Player;
import magicbattle.game.character.CharacterClass.CharacterClasses;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.lang.reflect.Array;

public class PlayerSelect extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9007465928995690156L;
	
	private JPanel contentPane;
	private JTextField txtName1;
	private JTextField txtName2;
	private JComboBox cmbClass1;
	private JComboBox cmbClass2;

	/**
	 * Create the frame.
	 */
	public PlayerSelect(final Game game) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 407, 188);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		// ---------------------------------------------------
		// Player 1
		// ---------------------------------------------------

		JPanel panel1 = new JPanel();
		panel1.setBounds(0, 0, 189, 107);
		contentPane.add(panel1);
		panel1.setLayout(null);

		JLabel lblPlayer1 = new JLabel("Player 1");
		lblPlayer1.setBounds(10, 11, 80, 14);
		panel1.add(lblPlayer1);

		txtName1 = new JTextField();
		txtName1.setBounds(88, 33, 86, 20);
		panel1.add(txtName1);
		txtName1.setColumns(10);

		JLabel lblName1 = new JLabel("Name:");
		lblName1.setBounds(10, 36, 46, 14);
		panel1.add(lblName1);

		cmbClass1 = new JComboBox();
		cmbClass1.setModel(this.viewCharacterClasses());
		cmbClass1.setBounds(88, 64, 86, 20);
		panel1.add(cmbClass1);

		JLabel lblKlasse1 = new JLabel("Klasse:");
		lblKlasse1.setBounds(10, 67, 46, 14);
		panel1.add(lblKlasse1);

		// ---------------------------------------------------
		// Player 2
		// ---------------------------------------------------

		JPanel panel2 = new JPanel();
		panel2.setLayout(null);
		panel2.setBounds(199, 0, 189, 107);
		contentPane.add(panel2);

		JLabel lblPlayer2 = new JLabel("Player 2");
		lblPlayer2.setBounds(10, 11, 80, 14);
		panel2.add(lblPlayer2);

		txtName2 = new JTextField();
		txtName2.setColumns(10);
		txtName2.setBounds(88, 33, 86, 20);
		panel2.add(txtName2);

		JLabel lblName2 = new JLabel("Name:");
		lblName2.setBounds(10, 36, 46, 14);
		panel2.add(lblName2);

		cmbClass2 = new JComboBox();
		cmbClass2.setBounds(88, 64, 86, 20);
		cmbClass2.setModel(this.viewCharacterClasses());
		panel2.add(cmbClass2);

		JLabel lblKlasse2 = new JLabel("Klasse:");
		lblKlasse2.setBounds(10, 67, 46, 14);
		panel2.add(lblKlasse2);

		// ---------------------------------------------------
		//
		// ---------------------------------------------------
		JButton btnStart = new JButton("Start");
		btnStart.addActionListener(new ActionListener() {
			Player[] player = new Player[2];
			public void actionPerformed(ActionEvent arg0) {				
				player[0] = new Player(txtName1.getText(),
						returnCharacterClasses(cmbClass1.getSelectedIndex()));
				player[1] = new Player(txtName1.getText(),
						returnCharacterClasses(cmbClass1.getSelectedIndex()));
				game.setPlayer(player);
				
				try {
					GameGUI frame1 = new GameGUI(game);
					frame1.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				try {
					GameGUI frame2 = new GameGUI(game);
					frame2.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		btnStart.setBounds(299, 118, 89, 23);
		contentPane.add(btnStart);
	}

	private DefaultComboBoxModel viewCharacterClasses() {

		return new DefaultComboBoxModel(new String[] { "Magier", "Krieger" });
	}

	private CharacterClasses returnCharacterClasses(int id) {

		switch (id) {
		case 0:
			// mage
			return CharacterClasses.MAGE;
		case 1:
			// warrior
			return CharacterClasses.WARRIOR;
		default:
			return CharacterClasses.MAGE;
		}
	}
}
